package com.un.ebs.businessObject.service.impl;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import cn.hutool.core.util.EnumUtil;
import com.un.ebs.businessObject.service.SQLStrategy;
import com.un.ebs.core.enums.*;
import com.un.ebs.core.exception.BusinessException;
import com.un.ebs.core.metadata.EnumInfo;
import com.un.ebs.core.util.PackageUtil;
import com.un.ebs.core.util.SecurityUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.un.ebs.core.metadata.Query;
import com.un.ebs.core.metadata.QueryCondition;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.sys.config.HandlerDbType;
import com.un.ebs.sys.domain.Column;
import com.un.ebs.sys.enums.DisplayEnums;

import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author zc
 * @ClassName SQLServerStrategy
 * @Description SqlService策略
 * @Date 2021/1/19
 */
@Service
@HandlerDbType(DbTypeEnum.sqlserver)
@Slf4j
public class SQLServerStrategy implements SQLStrategy {

    @Override
    public String listSQL(String tableName, String sortName, Map<String, Object> params, List<Column> columnList) {
        // 定义三个sql
        StringBuffer baseSQL = new StringBuffer(" SELECT ");
        StringBuffer conditionSQL = new StringBuffer(" WHERE 1=1 ");
        StringBuffer orderBySQL = new StringBuffer();

        // 是否删除、条件添加
        List<String> collect = columnList.stream().map(x -> x.getColumnName()).collect(Collectors.toList());
        boolean delFlag = collect.contains(DisplayEnums.IS_DELETE.getColumnName());
        columnList = columnList.stream().filter(x -> DisplayEnums.getEnumByColumnName(x.getColumnName()) == null)
                .collect(Collectors.toList());
        if (delFlag) {
            conditionSQL.append(" AND " + DisplayEnums.IS_DELETE.getColumnName() + " = 0");
        }

        // 判断排序
        boolean sortFlag = collect.contains(sortName);
        if (sortFlag) {
            orderBySQL.append(" ORDER BY " + sortName);
        }

        for (int i = 0; i < columnList.size(); i++) {
            Column column = columnList.get(i);
            String columnName = column.getColumnName();
            Object param = null;

            if (!CollectionUtil.isEmpty(params)) {
                param = params.get(columnName);
            }

            if (i != columnList.size() - 1) {
                baseSQL.append(columnName += ",");
            } else {
                baseSQL.append(columnName);
            }

            String paramValue = (param == null ? "" : param.toString());
            if (!StringUtils.isNullOrEmpty(paramValue)) {
                String condition = "";
                DataTypeEnum enumByCode = column.getColumnType();
                // 整数型，布尔型，《等于处理》
                if (DataTypeEnum.INT.equals(enumByCode) || DataTypeEnum.BOOLEAN.equals(enumByCode)) {

                    condition = " AND " + column.getColumnName() + " = " + param;

                } else if (DataTypeEnum.STRING.equals(enumByCode)) { // 字符型 处理为like

                    condition = " AND " + column.getColumnName() + " LIKE '%";
                    condition += param + "%'";

                } else if (DataTypeEnum.DATE.equals(enumByCode)) {
                    // 格式化日期
                    condition = " AND " + column.getColumnName() + "='" + param + "'";
                } else if (DataTypeEnum.OBJECT.equals(enumByCode)) {
                    // 对象类型
                    condition = " AND " + column.getColumnName() + "='" + param + "'";
                }
                conditionSQL.append(condition);
            }
        }

        baseSQL.append(" FROM " + tableName + " ").append(conditionSQL).append(orderBySQL);
        log.debug("listSQL:" + baseSQL.toString());
        return baseSQL.toString();
    }

    @Override
    public String pageListSQL(String tableName, String sortName, Integer pageIndex, Integer pageSize,
                              Map<String, Object> params, List<Column> columnList) {
        StringBuffer baseSQL = new StringBuffer("WITH selectTemp as (SELECT top 100 PERCENT row_number() over (order by " + sortName + " asc) as __row_number__, ");
        StringBuffer conditionSQL = new StringBuffer(" WHERE 1=1 ");
        StringBuffer orderBySQL = new StringBuffer(" order by " + sortName + " asc) select * from selectTemp where __row_number__ between " + pageIndex + " and " + pageSize + " order by __row_number__ asc");

        // 逻辑删除
        if (columnList.stream().filter(s -> s.getColumnName().equals(DisplayEnums.IS_DELETE.getColumnName())).count() == 1) {
            conditionSQL.append(" AND " + DisplayEnums.IS_DELETE.getColumnName() + " = 0");
        }
        // 租户过滤
        if (columnList.stream().filter(s -> s.getColumnName().equals(DisplayEnums.TENANT_ID.getColumnName())).count() == 1) {
            conditionSQL.append(" AND " + DisplayEnums.TENANT_ID.getColumnName() + " = '" + SecurityUtils.getTenantId() + "'");
        }

        //其它控制字段过滤
        //columnList = columnList.stream().filter(x -> DisplayEnums.getEnumByColumnName(x.getColumnName()) == null).collect(Collectors.toList());

        for (int i = 0; i < columnList.size(); i++) {
            Column column = columnList.get(i);
            String columnName = column.getColumnName();
            Object param = null;

            if (!CollectionUtil.isEmpty(params)) {
                param = params.get(columnName);
            }

            if (i != columnList.size() - 1) {
                baseSQL.append(columnName += ", ");
            } else {
                baseSQL.append(columnName);
            }

            String paramValue = (param == null ? "" : param.toString());
            if (!StringUtils.isNullOrEmpty(paramValue)) {
                String condition = "";
                DataTypeEnum enumByCode = column.getColumnType();
                // 整数型，布尔型，《等于处理》
                switch (enumByCode) {
                    case INT:
                    case BOOLEAN:
                        condition = " AND " + column.getColumnName() + " = " + param;
                        break;
                    case STRING:
                        condition = " AND " + column.getColumnName() + " LIKE '%";
                        condition += param + "%'";
                        break;
                    case DATE:
                    case DATETIME:
                        // 格式化日期
                        condition = " AND convert(varchar(10)," + column.getColumnName() + ", 120) = convert(varchar(10),'" + param + "', 120)";
                        break;
                    default:
                        condition = " AND " + column.getColumnName() + "='" + param + "'";
                        break;
                }
                conditionSQL.append(condition);
            }
        }
        baseSQL.append(" FROM " + tableName + " ").append(conditionSQL).append(orderBySQL);
        return baseSQL.toString();
    }

    @Override
    public String pageListCount(String tableName, String sortName, Map<String, Object> params,
                                List<Column> columnList) {
        StringBuffer baseSQL = new StringBuffer(" SELECT count(*) ");
        StringBuffer conditionSQL = new StringBuffer(" WHERE 1=1 ");
        // 是否删除、条件添加
        boolean is_del = columnList.stream().map(x -> x.getColumnName()).collect(Collectors.toList())
                .contains(DisplayEnums.IS_DELETE.getColumnName());
        columnList = columnList.stream().filter(x -> DisplayEnums.getEnumByColumnName(x.getColumnName()) == null)
                .collect(Collectors.toList());
        if (is_del) {
            conditionSQL.append(" AND " + DisplayEnums.IS_DELETE.getColumnName() + " = 0");
        }

        for (int i = 0; i < columnList.size(); i++) {
            Column column = columnList.get(i);
            String columnName = column.getColumnName();
            Object param = null;

            if (!CollectionUtil.isEmpty(params)) {
                param = params.get(columnName);
            }

            String paramValue = (param == null ? "" : param.toString());
            if (!StringUtils.isNullOrEmpty(paramValue)) {
                String condition = "";
                DataTypeEnum enumByCode = column.getColumnType();
                // 整数型，布尔型，《等于处理》
                if (DataTypeEnum.INT.equals(enumByCode) || DataTypeEnum.BOOLEAN.equals(enumByCode)) {

                    condition = " AND " + column.getColumnName() + " = " + param;

                } else if (DataTypeEnum.STRING.equals(enumByCode)) { // 字符型 处理为like

                    condition = " AND " + column.getColumnName() + " LIKE '%";
                    condition += param + "%'";

                } else if (DataTypeEnum.DATE.equals(enumByCode)) {
                    // 格式化日期
                    condition = " AND " + column.getColumnName() + "='" + param + "'";
                } else if (DataTypeEnum.OBJECT.equals(enumByCode)) {
                    // 对象类型
                    condition = " AND " + column.getColumnName() + "='" + param + "'";
                }
                conditionSQL.append(condition);
            }
        }

        baseSQL.append(" FROM " + tableName + " ").append(conditionSQL);
        log.debug("pageListCount:" + baseSQL.toString());
        return baseSQL.toString();
    }

    @Override
    public String getById(String tableName, String id, List<Column> columnList) {
        StringBuffer baseSQL = new StringBuffer(" SELECT ");
        StringBuffer conditionSQL = new StringBuffer(" WHERE 1=1 AND id=" + "'" + id + "'");

        Optional<Column> colTenantId = columnList.stream()
                .filter(x -> x.getColumnName().equals(DefaultFieldEnum.tenant_id.getValue())).findFirst();
        if (colTenantId.get() != null && org.apache.commons.lang.StringUtils.isNotBlank(colTenantId.get().getId())) {
            conditionSQL.append(" AND " + DefaultFieldEnum.tenant_id.getValue() + " = '" + SecurityUtils.getTenantId() + "'");
        }

        // 是否删除、条件添加
        boolean is_del = columnList.stream().map(x -> x.getColumnName()).collect(Collectors.toList())
                .contains(DisplayEnums.IS_DELETE.getColumnName());
        columnList = columnList.stream().filter(x -> DisplayEnums.getEnumByColumnName(x.getColumnName()) == null)
                .collect(Collectors.toList());
        if (is_del) {
            conditionSQL.append(" AND " + DisplayEnums.IS_DELETE.getColumnName() + " = 0");
        }

        for (int i = 0; i < columnList.size(); i++) {
            Column column = columnList.get(i);
            String columnName = column.getColumnName();
            if (i != columnList.size() - 1) {
                baseSQL.append(columnName + ",");
            } else {
                baseSQL.append(columnName);
            }
        }
        baseSQL.append(" FROM " + tableName + " ").append(conditionSQL);
        return baseSQL.toString();
    }

    @Override
    public String add(String tableName, JSONObject json, List<Column> columnList) {
        List<String> collect = columnList.stream().sorted(Comparator.comparing(s -> s.getSort())).map(x -> x.getColumnName()).collect(Collectors.toList());

        if (collect.contains(DisplayEnums.IS_DELETE.getColumnName())) {
            json.put(DisplayEnums.IS_DELETE.getColumnName(), "0");
        }

        StringBuffer insertSQL = new StringBuffer(" insert into " + tableName + " ( ");

        StringBuffer keys = new StringBuffer();
        StringBuffer values = new StringBuffer();

        for (int i = 0; i < columnList.size(); i++) {
            Column column = columnList.get(i);
            String key = column.getColumnName();
            if (column.getIsPk()) {
                json.put(key, json.getString("id"));
            }
            String value = json.getString(column.getColumnName());

            DataTypeEnum enumByCode = column.getColumnType();
            if (value == null) {
                continue;
            }

            if (i != columnList.size() - 1) {
                if (DataTypeEnum.INT.equals(enumByCode) && column.getRefType() == ColumnRefTypeEnum.Enum) {
                    try {
                        Optional<EnumInfo> enumValue = PackageUtil.getEnums(Class.forName(column.getRefEnumType())).stream().filter(s -> s.getName().equals(value)).findFirst();
                        if (enumValue.isPresent()) {
                            values.append(enumValue.get().getCode()+",");
                        } else {
                            throw new BusinessException("枚举值不正确。");
                        }
                    } catch (ClassNotFoundException e) {
                        throw new BusinessException(e.getMessage());
                    }
                } else if (DataTypeEnum.INT.equals(enumByCode) || DataTypeEnum.BOOLEAN.equals(enumByCode)) {
                    values.append(value + ",");
                } else {
                    values.append("'" + value + "'" + ",");
                }
                keys.append(key + ",");
            } else {
                if (DataTypeEnum.INT.equals(enumByCode) && column.getRefType() == ColumnRefTypeEnum.Enum) {
                    try {
                        Optional<EnumInfo> enumValue = PackageUtil.getEnums(Class.forName(column.getRefEnumType())).stream().filter(s -> s.getName().equals(value)).findFirst();
                        if (enumValue.isPresent()) {
                            values.append(enumValue.get().getCode());
                        } else {
                            throw new BusinessException("枚举值不正确。");
                        }
                    } catch (ClassNotFoundException e) {
                        throw new BusinessException(e.getMessage());
                    }
                } else if (DataTypeEnum.INT.equals(enumByCode) || DataTypeEnum.BOOLEAN.equals(enumByCode)) {
                    values.append(value);
                } else {
                    values.append("'" + value + "'");
                }
                keys.append(key);
            }

        }

        if (keys.lastIndexOf(",") == keys.length() - 1) {
            insertSQL.append(keys.substring(0, keys.length() - 1));

        } else {
            insertSQL.append(keys);
        }
        insertSQL.append(") values (");

        if (values.lastIndexOf(",") == values.length() - 1) {
            insertSQL.append(values.substring(0, values.length() - 1));
        } else {
            insertSQL.append(values);
        }
        insertSQL.append(")");
        return insertSQL.toString();
    }

    @Override
    public String update(String tableName, JSONObject json, List<Column> columnList) {
        StringBuffer updateSQL = new StringBuffer(" update " + tableName);
        StringBuffer conditionSQL = new StringBuffer(" WHERE 1=1 AND id=" + "'" + json.get("id") + "'");
        StringBuffer centerSQL = new StringBuffer();

        Optional<Column> colTenantId = columnList.stream()
                .filter(x -> x.getColumnName().equals(DefaultFieldEnum.tenant_id.getValue())).findFirst();
        if (colTenantId.get() != null) {
            conditionSQL
                    .append(" AND " + DefaultFieldEnum.tenant_id.getValue() + " = '" + colTenantId.get().getId() + "'");
        }

        boolean is_del = columnList.stream().map(x -> x.getColumnName()).collect(Collectors.toList())
                .contains(DisplayEnums.IS_DELETE.getColumnName());
        columnList = columnList.stream().filter(x -> DisplayEnums.getEnumByColumnName(x.getColumnName()) == null)
                .collect(Collectors.toList());
        if (is_del) {
            conditionSQL.append(" AND " + DisplayEnums.IS_DELETE.getColumnName() + " = 0");
        }

        for (int i = 0; i < columnList.size(); i++) {
            Column column = columnList.get(i);
            String key = column.getColumnName();
            String value = json.getString(column.getColumnName());
            DataTypeEnum enumByCode = column.getColumnType();

            if (value != null) {
                if (i == 0 || StringUtils.isNullOrEmpty(centerSQL.toString())) {
                    if (DataTypeEnum.INT.equals(enumByCode) || DataTypeEnum.BOOLEAN.equals(enumByCode)) {
                        centerSQL.append(" set " + key + "=" + value);
                    } else {
                        centerSQL.append(" set " + key + "=" + "'" + value + "'");
                    }
                } else if (i > 0) {
                    if (DataTypeEnum.INT.equals(enumByCode) || DataTypeEnum.BOOLEAN.equals(enumByCode)) {
                        centerSQL.append("," + key + "=" + value);
                    } else {
                        centerSQL.append("," + key + "=" + "'" + value + "'");
                    }
                }
            }
        }

        updateSQL.append(centerSQL).append(conditionSQL);
        return updateSQL.toString();
    }

    @Override
    public String delete(String tableName, String id, Boolean supportLogicDelete, String tenantId) {
        StringBuffer sql = new StringBuffer();
        if (supportLogicDelete) {
            sql.append("update " + tableName + " set is_delete = 1 WHERE 1=1 AND id=" + "'" + id + "'");
        } else {
            sql.append("delete from " + tableName + " WHERE 1=1 AND id=" + "'" + id + "'");
        }

        if (tenantId != null) {
            sql.append(" AND " + DefaultFieldEnum.tenant_id.getValue() + " = '" + tenantId + "'");
        }

        return sql.toString();
    }

    @Override
    public String querySQL(String tableName, Query query, List<Column> columnList) {
        StringBuffer baseSQL = new StringBuffer(" SELECT ");
        StringBuffer conditionSQL = new StringBuffer(" WHERE 1=1 ");
        StringBuffer pageSQL = new StringBuffer(" offset ");
        StringBuffer orderBySQL = new StringBuffer();
        pageSQL.append("((" + query.getPageIndex() + "-1)*" + query.getPageSize() + ") rows fetch next "
                + query.getPageSize() + " rows only");

        List<QueryCondition> conditions = query.getConditions();

        Map<String, QueryCondition> conditionMap = conditions.stream()
                .collect(Collectors.toMap(QueryCondition::getKey, Function.identity()));

        // 是否删除、条件添加
        List<String> collect = columnList.stream().map(x -> x.getColumnName()).collect(Collectors.toList());
        boolean is_del = collect.contains(DisplayEnums.IS_DELETE.getColumnName());
        columnList = columnList.stream().filter(x -> DisplayEnums.getEnumByColumnName(x.getColumnName()) == null)
                .collect(Collectors.toList());

        if (is_del) {
            conditionSQL.append(" AND " + DisplayEnums.IS_DELETE.getColumnName() + " = 0");
        }

        // 判断排序
        boolean sortFlag = collect.contains(query.getSortName());
        if (sortFlag) {
            orderBySQL.append(" ORDER BY " + query.getSortName());
        } else {
            orderBySQL.append(" ORDER BY " + collect.get(0));
        }

        for (int i = 0; i < columnList.size(); i++) {
            Column column = columnList.get(i);
            String columnName = column.getColumnName();
            if (i != columnList.size() - 1) {
                baseSQL.append(columnName).append(",");
            } else {
                baseSQL.append(columnName);
            }
            QueryCondition queryCondition = conditionMap.get(column.getColumnName());

            // 如果查询条件为空
            if (queryCondition == null || CollectionUtil.isEmpty(queryCondition.getValues())) {
                continue;
            }
            DataTypeEnum enumByCode = column.getColumnType();
            OperatorEnum operatorEnum = OperatorEnum.getEnumByCode(Integer.valueOf(queryCondition.getOperator()));

            if (OperatorEnum.TYPE_NE.equals(operatorEnum)) {
                if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
                        || DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
                    conditionSQL.append(" AND " + columnName + "<>" + "'" + queryCondition.getValues().get(0) + "'");
                } else {
                    conditionSQL.append(" AND " + columnName + "<>" + queryCondition.getValues().get(0));
                }
            } else if (OperatorEnum.TYPE_EQ.equals(operatorEnum)) {
                if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
                        || DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
                    conditionSQL.append(" AND " + columnName + "=" + "'" + queryCondition.getValues().get(0) + "'");
                } else {
                    conditionSQL.append(" AND " + columnName + "=" + queryCondition.getValues().get(0));
                }
            } else if (OperatorEnum.TYPE_GT.equals(operatorEnum)) {
                if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
                        || DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
                    conditionSQL.append(" AND " + columnName + ">" + "'" + queryCondition.getValues().get(0) + "'");
                } else {
                    conditionSQL.append(" AND " + columnName + ">" + queryCondition.getValues().get(0));
                }
            } else if (OperatorEnum.TYPE_GE.equals(operatorEnum)) {
                if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
                        || DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
                    conditionSQL.append(" AND " + columnName + ">=" + "'" + queryCondition.getValues().get(0) + "'");
                } else {
                    conditionSQL.append(" AND " + columnName + ">=" + queryCondition.getValues().get(0));
                }
            } else if (OperatorEnum.TYPE_LT.equals(operatorEnum)) {
                if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
                        || DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
                    conditionSQL.append(" AND " + columnName + "<" + "'" + queryCondition.getValues().get(0) + "'");
                } else {
                    conditionSQL.append(" AND " + columnName + "<" + queryCondition.getValues().get(0));
                }
            } else if (OperatorEnum.TYPE_LE.equals(operatorEnum)) {
                if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
                        || DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
                    conditionSQL.append(" AND " + columnName + "<=" + "'" + queryCondition.getValues().get(0) + "'");
                } else {
                    conditionSQL.append(" AND " + columnName + "<=" + queryCondition.getValues().get(0));
                }
            } else if (OperatorEnum.TYPE_LIKE.equals(operatorEnum)) {
                conditionSQL.append(" AND " + columnName + " LIKE" + "'%" + queryCondition.getValues().get(0) + "%'");
            } else if (OperatorEnum.TYPE_NOT_LIKE.equals(operatorEnum)) {
                conditionSQL
                        .append(" AND " + columnName + " NOT LIKE" + "'%" + queryCondition.getValues().get(0) + "%'");
            } else if (OperatorEnum.TYPE_IN.equals(operatorEnum)) {
                // 构造in条件
                List<String> values = queryCondition.getValues();
                String inStr = "";
                for (int j = 0; j < values.size(); j++) {
                    if (j != values.size() - 1) {
                        inStr += "'" + values.get(j) + "'" + ",";
                    } else {
                        inStr += "'" + values.get(j) + "'";
                    }
                }
                conditionSQL.append(" AND " + columnName + " IN (" + inStr + ")");
            } else if (OperatorEnum.TYPE_SI.equals(operatorEnum)) {
                if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
                    conditionSQL.append(" AND " + columnName + ">=" + "'" + queryCondition.getValues().get(0) + "'");
                    conditionSQL.append(" AND " + columnName + ">=" + "'" + queryCondition.getValues().get(1) + "'");

                } else {
                    conditionSQL.append(" AND " + columnName + ">=" + queryCondition.getValues().get(0));
                    conditionSQL.append(" AND " + columnName + "<=" + queryCondition.getValues().get(1));
                }
            }

        }

        baseSQL.append(" FROM " + tableName + " ").append(conditionSQL).append(orderBySQL).append(pageSQL).append(" ;");
        log.debug("querySQL：" + baseSQL.toString());
        return baseSQL.toString();
    }

    @Override
    public String queryCount(String tableName, Query query, List<Column> columnList) {
        StringBuffer baseSQL = new StringBuffer(" SELECT COUNT(1) ");
        StringBuffer conditionSQL = new StringBuffer(" WHERE 1=1 ");

        // 是否删除、条件添加
        boolean is_del = columnList.stream().map(x -> x.getColumnName()).collect(Collectors.toList())
                .contains(DisplayEnums.IS_DELETE.getColumnName());
        columnList = columnList.stream().filter(x -> DisplayEnums.getEnumByColumnName(x.getColumnName()) == null)
                .collect(Collectors.toList());
        if (is_del) {
            conditionSQL.append(" AND " + DisplayEnums.IS_DELETE.getColumnName() + " = 0");
        }

        List<QueryCondition> conditions = query.getConditions();

        Map<String, QueryCondition> conditionMap = conditions.stream()
                .collect(Collectors.toMap(QueryCondition::getKey, Function.identity()));

        for (int i = 0; i < columnList.size(); i++) {
            Column column = columnList.get(i);
            String columnName = column.getColumnName();
            QueryCondition queryCondition = conditionMap.get(column.getColumnName());

            // 如果查询条件为空
            if (queryCondition == null || CollectionUtil.isEmpty(queryCondition.getValues())) {
                continue;
            }
            DataTypeEnum enumByCode = column.getColumnType();
            OperatorEnum operatorEnum = OperatorEnum.getEnumByCode(Integer.valueOf(queryCondition.getOperator()));

            if (OperatorEnum.TYPE_NE.equals(operatorEnum)) {
                if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
                        || DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
                    conditionSQL.append(" AND " + columnName + "<>" + "'" + queryCondition.getValues().get(0) + "'");
                } else {
                    conditionSQL.append(" AND " + columnName + "<>" + queryCondition.getValues().get(0));
                }
            } else if (OperatorEnum.TYPE_EQ.equals(operatorEnum)) {
                if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
                        || DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
                    conditionSQL.append(" AND " + columnName + "=" + "'" + queryCondition.getValues().get(0) + "'");
                } else {
                    conditionSQL.append(" AND " + columnName + "=" + queryCondition.getValues().get(0));
                }
            } else if (OperatorEnum.TYPE_GT.equals(operatorEnum)) {
                if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
                        || DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
                    conditionSQL.append(" AND " + columnName + ">" + "'" + queryCondition.getValues().get(0) + "'");
                } else {
                    conditionSQL.append(" AND " + columnName + ">" + queryCondition.getValues().get(0));
                }
            } else if (OperatorEnum.TYPE_GE.equals(operatorEnum)) {
                if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
                        || DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
                    conditionSQL.append(" AND " + columnName + ">=" + "'" + queryCondition.getValues().get(0) + "'");
                } else {
                    conditionSQL.append(" AND " + columnName + ">=" + queryCondition.getValues().get(0));
                }
            } else if (OperatorEnum.TYPE_LT.equals(operatorEnum)) {
                if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
                        || DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
                    conditionSQL.append(" AND " + columnName + "<" + "'" + queryCondition.getValues().get(0) + "'");
                } else {
                    conditionSQL.append(" AND " + columnName + "<" + queryCondition.getValues().get(0));
                }
            } else if (OperatorEnum.TYPE_LE.equals(operatorEnum)) {
                if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)
                        || DataTypeEnum.OBJECT.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
                    conditionSQL.append(" AND " + columnName + "<=" + "'" + queryCondition.getValues().get(0) + "'");
                } else {
                    conditionSQL.append(" AND " + columnName + "<=" + queryCondition.getValues().get(0));
                }
            } else if (OperatorEnum.TYPE_LIKE.equals(operatorEnum)) {
                conditionSQL.append(" AND " + columnName + " LIKE" + "'%" + queryCondition.getValues().get(0) + "%'");
            } else if (OperatorEnum.TYPE_NOT_LIKE.equals(operatorEnum)) {
                conditionSQL
                        .append(" AND " + columnName + " NOT LIKE" + "'%" + queryCondition.getValues().get(0) + "%'");
            } else if (OperatorEnum.TYPE_IN.equals(operatorEnum)) {
                // 构造in条件
                List<String> values = queryCondition.getValues();
                String inStr = "";
                for (int j = 0; j < values.size(); j++) {
                    if (j != values.size() - 1) {
                        inStr += "'" + values.get(j) + "'" + ",";
                    } else {
                        inStr += "'" + values.get(j) + "'";
                    }
                }
                conditionSQL.append(" AND " + columnName + " IN (" + inStr + ")");
            } else if (OperatorEnum.TYPE_SI.equals(operatorEnum)) {
                if (DataTypeEnum.STRING.equals(enumByCode) || DataTypeEnum.DATE.equals(enumByCode)) { // 如果是字符串类型、或者日期类型则添加单引号
                    conditionSQL.append(" AND " + columnName + ">=" + "'" + queryCondition.getValues().get(0) + "'");
                    conditionSQL.append(" AND " + columnName + ">=" + "'" + queryCondition.getValues().get(1) + "'");

                } else {
                    conditionSQL.append(" AND " + columnName + ">=" + queryCondition.getValues().get(0));
                    conditionSQL.append(" AND " + columnName + "<=" + queryCondition.getValues().get(1));
                }
            }

        }

        baseSQL.append(" FROM " + tableName + " ").append(conditionSQL);
        log.info("queryCountSQL：" + baseSQL.toString());
        return baseSQL.toString();
    }
}
